package com.eqdd.yiqidian.ui;

import android.Manifest;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;

import com.eqdd.yiqidian.ImageChooseActivityCustom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.utils.ImageUtil;
import com.eqdd.yiqidian.utils.PicUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.tbruyelle.rxpermissions.RxPermissions;

import java.io.File;

public class ImageChooseActivity extends BaseActivity {
    private ImageChooseActivityCustom dataBinding;
    private String absolutePath;
    private boolean isCamera;
    private Uri outUri;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_image_choose);
        initTopTitleBar(View.VISIBLE, "选择图片");
        initTopRightText("确定", (v) -> {
            if (TextUtils.isEmpty(absolutePath)) {
                ToastUtil.showLong("你还没选图片呢");
            } else {
                Intent intent = new Intent();
                intent.putExtra(Config.IMAGE_PATH, absolutePath);
                setResult(Config.SUCCESS, intent);
                finish();
            }
        });
    }

    @Override
    public void initData() {
        isCamera = getIntent().getBooleanExtra(Config.ONLY_CAMERA, false);
        absolutePath = getIntent().getStringExtra(Config.IMAGE_PATH);
        if (!TextUtils.isEmpty(absolutePath)) {
            ImageUtil.setImage(new File(absolutePath), dataBinding.ivShow);
        }
    }

    @Override
    public void setView() {

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.bt_choose:
            case R.id.iv_show:
                RxPermissions.getInstance(this)
                        .request(Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE)
                        .subscribe(isGranted -> {
                            if (isGranted) {
                                System.out.println("获取到照相机权限");
                                if (isCamera) {
                                    PicUtil.camera(ImageChooseActivity.this);
                                } else {
                                    PicUtil.single(ImageChooseActivity.this);
                                }
                            } else {
                                ToastUtil.showShort(Config.PERMISSION_CAMERA_FAILURE);
                            }
                        });

                break;
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case PictureConfig.CHOOSE_REQUEST:
                    absolutePath = PictureSelector.obtainMultipleResult(data).get(0).getCompressPath();
                    ImageUtil.setImage(new File(absolutePath), dataBinding.ivShow);
                    break;
            }
        }
    }

}
